package com.ruoyi.web.controller.common;


import com.ruoyi.common.utils.MapperUtils;
import com.ruoyi.system.domain.BusModelAccessories;
import com.ruoyi.system.service.IBusModelAccessoriesService;
import com.ruoyi.system.service.impl.BusModelAccessoriesServiceImpl;
import com.ruoyi.web.controller.minio.MinioUtils;
import com.ruoyi.web.controller.minio.UploadFileVo;
import io.minio.ObjectStat;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/minio")
@Slf4j
public class MinioController {


    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private IBusModelAccessoriesService iBusModelAccessoriesService;

    /**
     * 上传图片 后续优化方案（点击上传按钮 先不存储数据库 先去minio里面查询 如果有 直接取 没有的话再上传 点击确认后 再上传到数据库 取消的话 就不更改当前头像）
     * @param multipartFiles
     * @return
     */
    @SneakyThrows
    @RequestMapping("upload")
    public String excelTest(@RequestParam("file") MultipartFile[] multipartFiles,
                            @RequestParam("fileName")String fileName)  {
        Map resultMap = new HashMap();
        log.info(fileName);
                try {
                    for (MultipartFile multipartFile:multipartFiles) {
                         UploadFileVo uploadFileVo = minioUtils.uploadFile(multipartFile, "esl-cy", fileName);
                         boolean success = uploadFileVo.isSuccess();
                         if (success){
                             String[] split = fileName.split("\\.");
                             List<BusModelAccessories> busModelAccessories1 = iBusModelAccessoriesService.selectBusModelAccessoriesByPartCode(split[0]);
                             for (BusModelAccessories busModelAccessories2:busModelAccessories1) {
                                 busModelAccessories2.setPictureUrl("http://106.14.10.107:9000/esl-cy/"+fileName);
                                 iBusModelAccessoriesService.updateBusModelAccessories(busModelAccessories2);
                             }
                             resultMap.put("code",200);
                             resultMap.put("data",uploadFileVo.getFileName());
                             resultMap.put("msg","上传成功!");
                        }else{
                             resultMap.put("code",400);
                             resultMap.put("msg","上传失败!");
                         }
                    }
                }catch (Exception e){
                    resultMap.put("code",500);
                    resultMap.put("msg","服务器异常 请稍后再试！!");
                }
        return MapperUtils.mapToJson(resultMap);
    }
}
